package com.opener.common.code.assistant;

import com.opener.common.code.assistant.gen.AssistantUtils;
import com.opener.common.code.assistant.model.Prop;
import com.opener.common.code.assistant.model.Tab;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author baboy
 */
public class WebMvcAssistant {

    private static Pattern FIELD_PATTERN = Pattern.compile("\\s*`(\\w+)`\\s+(\\w+)",Pattern.CASE_INSENSITIVE);
    private static Pattern COMMENT_PATTERN = Pattern.compile("COMMENT\\s+'([^']+)'",Pattern.CASE_INSENSITIVE);
    private static Pattern TAB_PATTERN = Pattern.compile("table\\s+`(\\w+)`",Pattern.CASE_INSENSITIVE);
    public Tab parseTable(String tabStr,String tableNamePrefx){
        Tab tab = new Tab();
        tab.setTableNamePrefix(tableNamePrefx);
        List<Prop> props = new ArrayList();
        String[] a = tabStr.split("\n");
        for (int i = 0; i< a.length; i++){
            String s = a[i];
            Matcher tabMatcher = TAB_PATTERN.matcher(s);
            if (tabMatcher.find()){
                String table = tabMatcher.group(1);
                tab.setTable(table);
                System.out.println("table:"+table);
            }
            Matcher m = FIELD_PATTERN.matcher(s);
            if (m.find()){
                String dbField = m.group(1);
                if (dbField.startsWith("c_res") || dbField.startsWith("i_res")){
                    continue;
                }
                String dbType = m.group(2);
                Prop p = new Prop();
                p.setDbField(dbField);
                p.setDbType(dbType);
                props.add(p);
                Matcher commentMatcher = COMMENT_PATTERN.matcher(s);
                if (commentMatcher.find()){
                    String comment = commentMatcher.group(1);
                    p.setComment(comment);
                }
                System.out.println("field:"+dbField+"|type:"+dbType+"|javaProp:"+p.getJavaProp()+"|javaType:"+p.getJavaType());
            }
        }
        tab.setProps(props);
        return tab;
    }
    public static void gen(String mod, Tab tab, boolean createFile){
        try {
            AssistantUtils.gen(mod, tab,createFile);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        String s = "CREATE TABLE `t_hr_dept` (\n" +
                "  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,\n" +
                "  `name` varchar(32) DEFAULT '' COMMENT '部门名称',\n" +
                "  `full_name` varchar(255) DEFAULT '',\n" +
                "  `sort_no` int(11) NOT NULL DEFAULT '0' COMMENT '排序序号',\n" +
                "  `parent_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '上级部门ID',\n" +
                "  `status` enum('normal','invalid') NOT NULL COMMENT '部门状态',\n" +
                "  `type` enum('other','sales','production','internet','hr','telesales') DEFAULT NULL COMMENT '部门类型，依次为：其他，业务，产品，互联网，人事，电销',\n" +
                "  `level` varchar(64) DEFAULT NULL COMMENT '部门级别',\n" +
                "  `grade` set('lev1','lev2','lev3') DEFAULT '' COMMENT 'lev1:总监级部门，lev2:经理级部门，lev3:主管级部门，每个大部门都是总监级如果其下没有经理级则本身兼经理级',\n" +
                "  `filiale` enum('quanzheng','huaxin','xiaozhu') DEFAULT NULL,\n" +
                "  `filiale_show` int(11) DEFAULT NULL COMMENT '分公司（用于对外展示）,默认子继承父 (字典)',\n" +
                "  `city` varchar(64) DEFAULT NULL COMMENT '区域',\n" +
                "  `dd_id` int(11) DEFAULT NULL,\n" +
                "  `note` varchar(255) DEFAULT NULL COMMENT '部门简介',\n" +
                "  `create_uid` int(10) DEFAULT '0',\n" +
                "  `create_time` datetime NOT NULL,\n" +
                "  `update_uid` int(10) DEFAULT '0',\n" +
                "  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n" +
                "  `c_res1` varchar(64) DEFAULT NULL,\n" +
                "  `c_res2` varchar(64) DEFAULT NULL,\n" +
                "  `c_res3` varchar(64) DEFAULT NULL,\n" +
                "  `i_res1` bigint(20) DEFAULT NULL,\n" +
                "  `i_res2` bigint(20) DEFAULT NULL,\n" +
                "  `i_res3` bigint(20) DEFAULT NULL,\n" +
                "  PRIMARY KEY (`id`),\n" +
                "  UNIQUE KEY `id_isDeleted_Unique` (`id`) USING BTREE\n" +
                ") ENGINE=InnoDB AUTO_INCREMENT=11105 DEFAULT CHARSET=utf8mb4;";
        AssistantUtils.setup("/Users/baboy/idea/erp-hr", "com.yiju.erp.hr","mybatis-erp-hr");
        Tab tab = new WebMvcAssistant().parseTable(s,"t_hr");
        gen("dept", tab,false);

    }
}
